package com.sense.kukoo

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.launcher.ARouter
import com.sense.kukoo.common.file.FileConfig
import com.sense.kukoo.common.media.Kukoo
import com.sense.kukoo.common.night.DayNightManager
import com.sense.kukoo.common.push.FcmManager
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.utils.AppConfig
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.lib_logger.DebugLogger
import com.sense.kukoo.lib_logger.LoggerManager
import com.tencent.mmkv.MMKV
import io.alterac.blurkit.BlurKit

class KukooApp : Application() {

    override fun onCreate() {
        super.onCreate()
        ApplicationContext.init(this)
        initAppConfig()
        initFileConfig()
        initLogger()
        initARouter()
        initRepo()
        initActivityLifecycle()
        BlurKit.init(this)
        Kukoo.init(this)
        initDayNight()
        initPush()
    }

    private fun initAppConfig(){
        if (TextUtils.equals("release", BuildConfig.BUILD_TYPE)) {
            AppConfig.init(AppConfig.TYPE_RELEASE)
        } else {
            AppConfig.init(AppConfig.TYPE_DEBUG)
        }
    }

    private fun initFileConfig(){
        FileConfig.createDir(this)
    }

    private fun initARouter(){
        if (AppConfig.isDebugKey()) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
    }

    private fun initLogger() {
        LoggerManager.setLogger(DebugLogger())
        LoggerManager.setTag("TH")
    }

    private fun initRepo(){
        Repo.init(this, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, AppConfig.isReleaseKey())
    }

    private fun initDayNight(){
        DayNightManager.init()
    }

    private fun initPush(){
        FcmManager.init(this)
    }

    private fun initActivityLifecycle(){
        registerActivityLifecycleCallbacks(object :ActivityLifecycleCallbacks{
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                //
                LoggerManager.i("${activity.javaClass.name} --> Created")
            }

            override fun onActivityStarted(activity: Activity) {
                //
                LoggerManager.i("${activity.javaClass.name} --> Started")
            }

            override fun onActivityResumed(activity: Activity) {
                //
                LoggerManager.i("${activity.javaClass.name} --> Resumed")
            }

            override fun onActivityPaused(activity: Activity) {
                //
                LoggerManager.i("${activity.javaClass.name} --> Paused")
            }

            override fun onActivityStopped(activity: Activity) {
                //
                LoggerManager.i("${activity.javaClass.name} --> Stopped")
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
                //
                LoggerManager.i("${activity.javaClass.name} --> onActivitySaveInstanceState")
            }

            override fun onActivityDestroyed(activity: Activity) {
                //
                LoggerManager.i("${activity.javaClass.name} --> Destroyed")
            }

        })
    }
}